/*
 * ScreenHandler.java
 *
 * Created on July 30, 2007, 10:43 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.mar.bt.rd.server;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;


/**
 *
 * @author helio.rodrigues
 */
public class ScreenHandler {
    
    private Robot robot;
    
    private int deviceWidth;
    private int deviceHeight;
    private int desktopWidth;
    private int desktopHeight;
       
    /**
     * Creates a new instance of ScreenHandler
     *
     * @param robot
     */
    public ScreenHandler(Robot robot) {
        this.robot = robot;
        
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        desktopWidth = (int) screenDimension.getWidth();
        desktopHeight = (int) screenDimension.getHeight();
    }
    
    /**
     *
     * @param viewportX
     * @param viewportY
     * @param viewportWidth
     * @param viewportHeight
     * @throws java.io.IOException
     * @return
     */
    public byte[] createScreenCapture(int viewportX, int viewportY, int viewportWidth, int viewportHeight) throws IOException {
        /* creates the screenshot image */
        Image image = robot.createScreenCapture(new Rectangle(0, 0, desktopWidth, desktopHeight));
        
        BufferedImage bufferedImage = new BufferedImage(viewportWidth, viewportHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, viewportWidth, viewportHeight);
        graphics.drawImage(image, viewportX * (- 1), viewportY * (- 1), image.getWidth(null), image.getHeight(null), null);
        
        /* create a scaled instance */
        image = bufferedImage.getScaledInstance(deviceWidth, deviceHeight, BufferedImage.SCALE_SMOOTH);
        
        bufferedImage = new BufferedImage(deviceWidth, deviceHeight, BufferedImage.TYPE_INT_RGB);
        graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, deviceWidth, deviceHeight);
        graphics.drawImage(image, 0, 0, deviceWidth, deviceHeight, null);
        
        /* saves the screenshot on the disk */
        File file = new File(".idesktop.png");
        file.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ImageIO.write(bufferedImage, "png", fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        
        /* convert to bytes */
        byte[] imageBytes = new byte[(int) file.length()];
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(imageBytes);
        
        return imageBytes;
    }
    
    /**
     *
     * @return
     */
    public int getDeviceWidth() {
        return deviceWidth;
    }
    
    /**
     *
     * @param deviceWidth
     */
    public void setDeviceWidth(int deviceWidth) {
        this.deviceWidth = deviceWidth;
    }
    
    /**
     *
     * @return
     */
    public int getDeviceHeight() {
        return deviceHeight;
    }
    
    /**
     *
     * @param deviceHeight
     */
    public void setDeviceHeight(int deviceHeight) {
        this.deviceHeight = deviceHeight;
    }
    
}
